<?php
declare (strict_types = 1);

namespace app\admin\controller;
use app\admin\middleware\Auth;
use app\model\CategoryModel;
use app\model\DistrictCodesModel;
use app\model\QuotesModel;
use app\model\RedisModel;
use app\model\UserModel;
use app\model\UserUseDiscountCodeLogModel;
use think\App;
use think\facade\Db;

class UserUseDiscountCodeLog extends Base {

    protected $middleware = [Auth::class];
    private $model;

    private $req;

    private $page;

    private $limit;

    public function __construct(App $app)
    {
        parent::__construct($app);

        $this->req = request()->param();

        $this->page = isset($this->req['page']) ? $this->req['page'] : 1;

        $this->limit = isset($this->req['limit']) ? $this->req['limit'] : 999999;

        $this->model = new UserUseDiscountCodeLogModel();

         $this->isLogin();
    }

    /*
     * 获取店铺列表
     * */
    public function getList(){

        $where =[];
        if(!empty($this->req["code"])){
            $where["code"] = $this->req["code"];
        }

         if(!empty($this->req["uid"])){
            $where["uid"] = $this->req["uid"];
        }

        if(!empty($this->req["order_id"])){
            $where["order_id"] = $this->req["order_id"];
        }

        $list = $this->model->getList($where,$this->page,$this->limit,["updated_at"=>"DESC"]);

        $userModel = new UserModel();
        foreach ($list as &$one){
            $userInfo = $userModel->getUserInfoByUid($one["uid"]);
            $one["username"] = $userInfo["username"];
        }

        $total = $this->model->getTotal($where);

        return $this->succData($total,$list);

    }






    public function del()
    {
        if(!isset($this->req['id']) || empty($this->req['id'])){
            return $this->fail("请选择要删除的使用记录");
        }
        $res = $this->model->where("id",$this->req['id'])->save(["is_delete"=>1]);
        if($res){
            return $this->succ();
        }else{
            return $this->fail();
        }
    }
}
